package com.cars.satoken.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson2.JSON;
import com.cars.satoken.entities.Permission;
import com.cars.satoken.service.PermissionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: caoqianfan
 * @date: 2023/11/20 10:27
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @PostMapping("/add")
    public SaResult save(@RequestBody Permission permission) {
        if (permissionService.save(permission)) {
            return SaResult.ok();
        }
        return SaResult.error();
    }

    @DeleteMapping("/delete")
    public SaResult remove(@RequestBody Permission permission) {
        if (permissionService.removeById(permission)) {
            return SaResult.ok();
        }
        return SaResult.error();
    }

    @PutMapping("/update")
    public SaResult update(@RequestBody Permission permission) {
        if (permissionService.updateById(permission)) {
            return SaResult.ok();
        }
        return SaResult.error();
    }

    @GetMapping("/getById")
    public SaResult getById(@RequestParam Long id) {
        Permission permissionDO = permissionService.getById(id);
        if (permissionDO != null) {
            return SaResult.ok(JSON.toJSONString(permissionDO));
        }
        return SaResult.error("不存在该权限!");
    }

    @GetMapping("/list")
    public SaResult list() {
        List<Permission> permissionList = permissionService.list();
        if (!permissionList.isEmpty()) {
            return SaResult.ok(JSON.toJSONString(permissionList));
        }
        return SaResult.error("权限列表为空!");
    }
}